package cn.yeyiu.netonlinevideo.util;

import org.jsoup.nodes.DataNode;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import cn.yeyiu.util.bean.CommentInfo;
import cn.yeyiu.util.bean.ViewInfo;

public class BlogUtil {

    public static void buildView(List<ViewInfo> viewInfoList, List<Node> nodes) {
        ViewInfo viewInfo;
        int i=0;
        for (Node node : nodes) {
            String name = node.nodeName();
            viewInfo = new ViewInfo();
            viewInfo.viewName = name;
            switch (name) {
                case "h1":
                case "h2":
                    if(node.childNodeSize()>0){
                        viewInfo.text = node.childNode(0).toString().trim();
                        viewInfo.textColor = 0x000;
                        viewInfo.textSize = 20;
                        viewInfoList.add(viewInfo);
                    }
                    break;
                case "img":
                    viewInfo.url = node.attr("src");
                    viewInfoList.add(viewInfo);
                    break;
                case "#text":
                    viewInfo.text = node.toString().trim();
                    viewInfoList.add(viewInfo);
                    break;
                case "hr":
                    viewInfoList.add(viewInfo);
                    break;
                case "br":
                    break;
                default:// span div p table...
                    buildView(viewInfoList, node.childNodes());
                    break;
            }
        }
    }

    public static CommentInfo _buildComment(Element node){
        CommentInfo commentInfo = new CommentInfo();

        Elements divs = node.children();
        // first must be comment
        Element commentDiv = divs.get(0);
        Elements infos = commentDiv.selectXpath("./div/div/div");
        // header
        Element header = infos.get(0);
        commentInfo.header = header.selectXpath("./div/div[1]//img").attr("src");
        if (commentInfo.header.startsWith("//")){
            commentInfo.header = "https:"+commentInfo.header;
        }
        commentInfo.name = header.selectXpath("./div/div[2]/div[1]/div").text();
        commentInfo.time = header.selectXpath("./div/div[2]/div[2]/div").text();
        // comment
        Element comment = infos.get(1);
        commentInfo.comment = getText(comment);

        // size min=2
        if (divs.size()>2){
            commentInfo.subCommentList = new ArrayList<>();
            for (int i=2;i<divs.size();++i){
                commentInfo.subCommentList.add(_buildComment(divs.get(i)));
            }
        }

        return commentInfo;
    }

    public static void buildComment(List<CommentInfo> commentInfoList, List<Element> nodes){
        for (Element element: nodes){
            commentInfoList.add(_buildComment(element));
        }
    }
    public static String getText(Node rootNode){
        StringBuilder stringBuilder = new StringBuilder();
        List<Node> nodes = rootNode.childNodes();
        if (nodes.size()==0){
            return "";
        }
        for (Node node:nodes){
            if ("#text".equals(node.nodeName())){
                stringBuilder.append(node.toString().trim());
            }else if ("br".equals(node.nodeName())){
                stringBuilder.append("\n");
            }else {
                stringBuilder.append("\n");
                String text = getText(node);
                stringBuilder.append(text);
            }
        }
        return stringBuilder.toString().trim();
    }
}
